<?php
/********************************************************************************
 * File: index.php
 * Desc: The main page shows one photo along with the photos description, EXIF
 *       data and comments.
 * Args: id - int optional
 *          The photo id to display.  If none, most recent photo is shown.
 *
 * Change   Date        By          Description
 * 1        05/13/2007  mcarruth    Created
 ********************************************************************************/
require_once("inc/header.inc");

echo "<script type='text/javascript' src='js/mootools.js'></script>\n";

//
// If 'id' was not passed in grab the most recent photo
//	
if (!$_GET['id']) {
   $photos = $f->photos_search(array("user_id"=>$config_flickrUserId, "extras"=>"original_format,date_taken", "per_page"=>"1", "page"=>"1"));
   $id = $photos['photo'][0]['id'];
}
else {
   $id = $_GET['id'];
}

//
// Get the photo info, exif, content, comments, and sizes
//
$info = $f->photos_getInfo($id);
$exif = $f->photos_getExif($id);
$context = $f->photos_getContext($id);
$photoSizes = $f->photos_getSizes($id);
$prev = $context['prevphoto']['id'];
$next = $context['nextphoto']['id'];
$comments = $f->photos_comments_getList($id);
$dateTaken = date("F j, Y", strtotime($info['dates']['taken']));
$datePosted = date("F j, Y", $info['dates']['posted']);
$largestPhotoSize = "";

//
// The following array is used to determine what exif information to display
//
$exifLabels = array(
   "Make" => "", 
   "Model" => "", 
   "Software" => "", 
   "Exposure" => "", 
   "Aperture" => "",
   "Shutter Speed" => "",
   "ISO Speed" => "",
   "Flash" => "",
);

//
// Get the exif data specified above
//
foreach ($exif['exif'] as $exifItem) {
   if (array_key_exists($exifItem['label'], $exifLabels) && !$exifLabels[$exifItem['label']]) {
      $exifLabels[$exifItem['label']] = $exifItem['clean'] ? $exifItem['clean'] : $exifItem['raw'];
   }
}      

//
// Determine the largest available photo size: original, large, or medium ("")
//
foreach ($photoSizes as $photoSize) {
   if ("Large" == $photoSize['label']) {
      $largestPhotoSize = "Large";
      break;
   } else if ("Original" == $photoSize['label']) {
      $largestPhotoSize = "Original";
      break;
   }
}

echo "<div id='page_header'>\n";
echo "   <span id='page_title'>" . $info['title'] . "</span>\n";
echo "   <span id='page_nav'>\n";
echo "      <a href='index.php?id=" . $prev . "'>prev </a>\n";
if (0 < $next) {
   echo "      <a href='index.php?id=" . $next . "'>next</a>\n";
}
echo "   </span>\n";
echo "</div>\n";
echo "<div id='page'>\n";
echo "   <a href='index.php?id=" . $context['prevphoto']['id'] . "' alt='" . $info['title']  . "'>\n";
echo "      <img class='photo' src='" . $f->buildPhotoURL($info, $largestPhotoSize) . "' title='" . $info['title'] . "' alt='" . $info['title']  . "' />\n";
echo "   </a>\n";
echo "</div>\n";

echo "<div id='page_meta'>\n";
echo "   <span style='float:left'>" . $datePosted . "</span>\n";
echo "   <span style='float:right'>\n";
echo "      <a href='#' onclick='slider.toggle();'>Info & Comments (". $info['comments'] . ")</a>\n";
echo "   </span>\n";
echo "</div>\n";

echo "<div id='info_comments'>\n";

// description
if ($info['description']) {
   echo    "<div id='description'><h2>description</h2>" . $info['description'] . "</div>\n";
}

// exif
echo    "<div id='exif'><h2>exif</h2>\n";
echo "Date: " . $dateTaken . "<br/>\n";
foreach ($exifLabels as $key=>$value) {
   if ($value) {
      echo $key . ": " . $value . "<br/>\n";
   }
}      

// comments
if ($comments['comment']) {
   echo    "<div id='comments'><h2>comments</h2>\n";
   foreach ($comments['comment'] as $comment) {
      echo $comment['authorname'] . " says:<br/>\n";
      echo $comment['_content'] . "<br/><br/>\n";      
   }
echo    "</div>\n";
}

echo    "<br/><a href='" . $info['urls']['url'][0]['_content'] . "'>Leave a comment</a>\n";

echo "</div>\n";

echo "<script defer='true'>var slider = new Fx.Slide('info_comments', {duration: 500});slider.hide();</script>\n";

require_once("inc/footer.inc"); 

?>
